/*
 * HTTP Server Functions Header
 * Author: Qoder
 * Description: Header file for HTTP server functionality
 */

#ifndef SERVER_FUNCTIONS_H
#define SERVER_FUNCTIONS_H

#include <sys/socket.h>
#include <netinet/in.h>

#define BUFFER_SIZE 4096
#define HTTP_VERSION "HTTP/1.1"
#define SERVER_NAME "libcurl-httpd/1.0"

// HTTP status codes
#define HTTP_200_OK "200 OK"
#define HTTP_400_BAD_REQUEST "400 Bad Request"
#define HTTP_404_NOT_FOUND "404 Not Found"
#define HTTP_405_METHOD_NOT_ALLOWED "405 Method Not Allowed"
#define HTTP_500_INTERNAL_ERROR "500 Internal Server Error"

// HTTP methods
typedef enum {
    HTTP_GET,
    HTTP_POST,
    HTTP_PUT,
    HTTP_DELETE,
    HTTP_UNKNOWN
} http_method_t;

// HTTP request structure
typedef struct {
    http_method_t method;
    char *path;
    char *query_string;
    char *headers;
    char *body;
    int content_length;
} http_request_t;

// Client information structure
typedef struct {
    int socket_fd;
    struct sockaddr_in client_addr;
} client_info_t;

// Function prototypes
void *handle_client(void *arg);
int parse_http_request(const char *raw_request, http_request_t *request);
void free_http_request(http_request_t *request);
http_method_t get_http_method(const char *method_str);
const char *get_method_string(http_method_t method);

// Response functions
void send_http_response(int client_fd, const char *status, const char *content_type, 
                       const char *body, int body_length);
void send_json_response(int client_fd, const char *status, const char *json_data);
void send_file_response(int client_fd, const char *file_path);
void send_error_response(int client_fd, const char *status, const char *message);

// Route handlers
void handle_ping(int client_fd, http_request_t *request);
void handle_download(int client_fd, http_request_t *request);
void handle_cgi_request(int client_fd, http_request_t *request);
void handle_404(int client_fd, http_request_t *request);

// Utility functions
char *get_mime_type(const char *file_extension);
char *url_decode(const char *encoded);
int file_exists(const char *file_path);
long get_file_size(const char *file_path);

#endif // SERVER_FUNCTIONS_H